package lumis.doui.render;

import lumis.portal.PortalException;
import lumis.portal.stability.StableMinor;
import lumis.util.XmlUtil;

/**
 * Wraps a {@link CharSequence} and {@link IRenderable} instances into an object
 * that implements both interfaces.
 * @since 4.0.4
 * @version $Revision: 13082 $ $Date: 2011-05-27 16:03:12 -0300 (Fri, 27 May 2011) $
 */
@StableMinor(version = "6.0", sinceVersion = "4.0")
public class CharSequenceRenderableWrapper implements CharSequence, IRenderable
{
	/**
	 * The wrapped {@link CharSequence} object.
	 * @since 4.0.4
	 */
	protected CharSequence charSequence;

	/**
	 * The wrapped {@link IRenderable} object.
	 * @since 4.0.4
	 */
	protected IRenderable renderable;

	public CharSequenceRenderableWrapper(CharSequence charSequence, IRenderable renderable)
	{
		this(charSequence);
		this.renderable = renderable;
	}

	protected CharSequenceRenderableWrapper(CharSequence charSequence)
	{
		if (charSequence == null)
			throw new NullPointerException();
		this.charSequence = charSequence;
	}
	
	public char charAt(int index)
	{
		return charSequence.charAt(index);
	}

	public int length()
	{
		return charSequence.length();
	}

	public CharSequence subSequence(int start, int end)
	{
		return charSequence.subSequence(start, end);
	}

	@Override
	public String toString()
	{
		return charSequence.toString();
	}

	public String getRenderData(IRenderer renderer) throws PortalException
	{
		if (renderable != null)
			return renderable.getRenderData(renderer);
		else
			return XmlUtil.encodeXml(toString());
	}

	public String getDisplayString(IRenderer renderer) throws PortalException
	{
		if (renderable != null)
			return renderable.getDisplayString(renderer);
		else
			return toString();
	}
}
